﻿using Infrastructure.FileFunction.Operation;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.FileFunction.Serialize
{
    public interface IInitModel
    {
        void InitModel();
    }

    /// <summary>
    /// 
    /// </summary>
    public static class JsonSerializeUtil
    {
        /// <summary>
        /// 序列化为json字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string SerializeToJsonStr<T>(T model) where T : class
        {
            return JsonConvert.SerializeObject(model);
        }

        /// <summary>
        /// 序列化为文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="filePath"></param>
        public static void SerializeJson<T>(this T model, string filePath) where T : class
        {
            string s = filePath.Substring(0, filePath.LastIndexOf('\\'));
            FilesUtils.CreateDir(s);

            using (var fs = new FileStream(filePath, FileMode.Create))
            using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
            {
                JsonSerializer serializer = new JsonSerializer();
                JsonWriter writer = new JsonTextWriter(sw);
                serializer.Serialize(writer, model);
            }
        }

        /// <summary>
        /// 反序列化为类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <returns></returns>
        public static T DeserializeJsonStrOrDefault<T>(string content) where T : class
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(content);
        }

        /// <summary>
        /// 反序列化为类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <returns></returns>
        public static T DeserializeJsonStr<T>(string content) where T : class
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                throw new FormatException("反序列化的内容为空");
            }
            return JsonConvert.DeserializeObject<T>(content);
        }

        /// <summary>
        /// 反序列化为类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T DeserializeJson<T>(string filePath) where T : class
        {
            if (!FilesUtils.FileExistes(filePath))
            {
                throw new FileNotFoundException($"{filePath}不存在");
            }
            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                var sr = new StreamReader(fs, Encoding.UTF8);
                string str = sr.ReadToEnd();
                return JsonConvert.DeserializeObject<T>(str);
            }

        }

        /// <summary>
        /// 读取或初始化json文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath">文件路径</param>
        /// <param name="reCreate">重置文件内容</param>
        /// <returns></returns>
        public static T ReadOrInitJsonFile<T>(string filePath, bool reCreate = false) where T : class, IInitModel, new()
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException($"路径为空");
            }


            if (reCreate)
            {
                T t = InitModel<T>();
                t.SerializeJson(filePath);
                return t;
            }

            try
            {
                return DeserializeJson<T>(filePath);
            }
            catch (FileNotFoundException)
            {
                T t = InitModel<T>();
                t.SerializeJson(filePath);
                return t;
            }
        }

        private static T InitModel<T>() where T : class, IInitModel, new()
        {
            T t = new T();
            t.InitModel();
            return t;
        }
    }
}
